package newcoder_exp.justPractise;

import org.junit.Test;

public class NC57 {
    
    public class Solution {
        public int reverse (int x) {
            int cur, d = 0, t = x, res = 0, g = 1;
            boolean flag = true;
            //1.计算x的位数
            cur = x;
            while (cur != 0) {
                d++;
                cur /= 10;
            }
            if (x < 0) {
                flag = false;
                t = -t;
            }
            d = (int)Math.pow(10, d - 1);
            while (t != 0) {
                cur = t / d;
                t -= d * cur;
                d /= 10;
                if (flag && (res > 214748364 && cur > 0 || res == 214748364 && cur > 7)) {
                    return Integer.MAX_VALUE;
                } else if(!flag && (res > 214748364 && cur > 0 || res == 214748364 && cur > 8)) {
                    return Integer.MIN_VALUE;
                } else {
                    res += cur * g;
                }
                g *= 10;
            }
            return flag ? res : -res;
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        System.out.println(s.reverse(-214748364        ));  
    }
}
